<?php
/**
 * 管理员列表
 **/
include("../includes/common.php");
$title = '更多小程序配置';
include './head.php';
if ($islogin == 1) {
} else exit("<script language='javascript'>window.location.href='./login.php';</script>");
?>
<div class="container" style="padding-top:70px;">
    <div class="col-sm-12 col-md-10 center-block" style="float: none;">
        <div class="modal fade" align="left" id="search" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
             aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                                class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel">搜索更多小程序</h4>
                    </div>
                    <div class="modal-body">
                        <form action="gdlist.php" method="GET">
                            <input type="text" class="form-control" name="linkname" placeholder="请输入小程序名"><br/>
                            <input type="submit" class="btn btn-primary btn-block" value="搜索" style="background:#000000; color:#FFFFFF"></form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
        <?php
        if ($udata['uid'] != 1) showmsg('当前账号没有权限使用此功能', 3);

        $my = isset($_GET['my']) ? $_GET['my'] : null;

        if ($my == 'add') {
            echo '<div class="panel panel-primary">
<div class="panel-heading" style="background:#000000; color:#FFFFFF"><h3 class="panel-title">添加小程序</h3></div>';
            echo '<div class="panel-body">';
            echo '<form action="./gdlist.php?my=add_submit" method="POST">
<div class="form-group">
<label>小程序名称:</label><br>
<input type="text" class="form-control" name="linkname" value="木木文案馆" required>
</div>
<div class="form-group">
<label>小程序介绍:</label><br>
<input type="text" class="form-control" name="description" value="图片头像朋友圈套图尽在于此" required>
</div>
<div class="form-group">
<label>小程序ID:</label><br>
<input type="text" class="form-control" name="appid" value="wx547220efe5a13c03" required>
</div>
<div class="form-group">
<label>小程序图标URL:</label><br>
<input type="text" class="form-control" name="img" value="https://qmwa.qmniy.cn/img/qmnwag.png" required>
</div>
<input type="submit" class="btn btn-primary btn-block" value="确定添加" style="background:#000000; color:#FFFFFF"></form>';
            echo '<br/><a href="./gdlist.php">>>返回小程序列表</a>';
            echo '</div></div>';
        } elseif ($my == 'edit') {
            $id = intval($_GET['id']);
            $row = $DB->get_row("select * from auth_gengduo where id='$id' limit 1");
            echo '<div class="panel panel-primary">
<div class="panel-heading" style="background:#000000; color:#FFFFFF"><h3 class="panel-title">修改小程序信息</h3></div>';
            echo '<div class="panel-body">';
            echo '<form action="./gdlist.php?my=edit_submit&id=' . $id . '" method="POST">
<div class="form-group">
<label>小程序名称:</label><br>
<input type="text" class="form-control" name="linkname" value="'.$row['linkname'].'" required>
</div>
<div class="form-group">
<label>小程序介绍:</label><br>
<input type="text" class="form-control" name="description" value="' . $row['description'] . '" required>
</div>
<div class="form-group">
<label>小程序ID:</label><br>
<input type="text" class="form-control" name="appid" value="' . $row['appid'] . '" required>
</div>
<div class="form-group">
<label>小程序图标URL:</label><br>
<input type="text" class="form-control" name="img" value="' . $row['img'] . '" required>
</div>
<input type="submit" class="btn btn-primary btn-block" value="确定修改" style="background:#000000; color:#FFFFFF"></form>';
            echo '<br/><a href="./gdlist.php">>>返回小程序列表</a>';
            echo '</div></div>
<script>
var items = $("select[default]");
for (i = 0; i < items.length; i++) {
	$(items[i]).val($(items[i]).attr("default")||0);
}
</script>';
        } elseif ($my == 'add_submit') {
            $linkname= $_POST['linkname'];
            $description =  $_POST['description'];
            $appid=  $_POST['appid'];
            $img=  $_POST['img'];
            if ($linkname == NULL or $description == NULL or $appid == NULL or $img == NULL) {
                showmsg('保存错误,请确保每项都不为空!', 3);
            } else {
                $rows = $DB->get_row("select * from auth_gengduo where linkname='$linkname' limit 1");
                if ($rows)
                    showmsg('该小程序已存在！', 3);
                $sql = "insert into `auth_gengduo` (`linkname`,`description`,`appid`,`img`) values ('" . $linkname . "','" . $description . "','" . $appid . "','" . $img . "')";
                if ($DB->query($sql)) {
                    showmsg('添加小程序成功！<br/><br/><a href="./gdlist.php">>>返回小程序列表</a>', 1);
                } else
                    showmsg('添加小程序失败！' . $DB->error(), 4);
            }
        } elseif ($my == 'edit_submit') {
            $id = intval($_GET['id']);
            $rows = $DB->get_row("select * from auth_gengduo where id='$id' limit 1");
            if (!$rows) showmsg('当前记录不存在！', 3);
            $id = $_POST['id'];
            $linkname = $_POST['linkname'];
            $description = $_POST['description'];
            $appid = $_POST['appid'];
            $img= $_POST['img'];
            if ($linkname == NULL or $description == NULL or $appid == NULL or $img == NULLL) {
                showmsg('保存错误,请确保每项都不为空!', 3);
            } else {
                if ($DB->query("update auth_gengduo set 'linkname'='$linkname','description'='$description','appid'='$appid','img'='$img' where 'id'='{$id}'"))
                    showmsg('修改小程序成功！<br/><br/><a href="./gdlist.php">>>返回小程序列表</a>', 1);
                else
                    showmsg('修改小程序失败！' . $DB->error(), 4);
            }
        } elseif ($my == 'delete') {
            $id = intval($_GET['id']);
            $sql = "DELETE FROM auth_gengduo WHERE id='$id'";
            if ($DB->query($sql))
                showmsg('删除成功！<br/><br/><a href="./gdlist.php">>>返回小程序列表</a>', 1);
            else
                showmsg('删除失败！' . $DB->error(), 4);
        } else {

            $numrows = $DB->count("SELECT count(*) from auth_gengduo");
            if (isset($_GET['id'])) {
                $sql = " id={$_GET['id']}";
            } elseif (isset($_GET['linkname'])) {
                $sql = " linkname='{$_GET['linkname']}'";
            } else {
                $sql = " 1";
            }
            $con = '系统共有 <b>' . $numrows . '</b> 个小程序<br/>
            <a href="./gdlist.php?my=add" class="btn btn-primary" style="background:#000000; color:#FFFFFF">添加小程序</a>&nbsp;
            <a href="#" data-toggle="modal" data-target="#search" id="search" class="btn btn-success" style="background:#000000; color:#FFFFFF" >搜索</a>';

            echo '<div class="alert alert-info" style="background:#000000; color:#FFFFFF">';
            echo $con;
            echo '</div>';

            ?>
            <div class="table-responsive">
                <table class="table table-striped" style="background:#FFFFFF; color:#000000">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>小程序名称</th>
                        <th>小程序介绍</th>
                        <th>小程序ID</th>
                        <th>小程序图标</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $pagesize = 30;
                    $pages = intval($numrows / $pagesize);
                    if ($numrows % $pagesize) {
                        $pages++;
                    }
                    if (isset($_GET['page'])) {
                        $page = intval($_GET['page']);
                    } else {
                        $page = 1;
                    }
                    $offset = $pagesize * ($page - 1);

                    $rs = $DB->query("SELECT * FROM auth_gengduo WHERE{$sql} order by id desc limit $offset,$pagesize");
                    while ($res = $DB->fetch($rs)) {
                        echo '<tr><td><b>' . $res['id'] . '</b></td><td>' . $res['linkname'] . '</td><td>' . $res['description'] . '</td><td>' . $res['appid'] . '</td><td>' . $res['img'] . '</td><td><a href="./gdlist.php?my=edit&id=' . $res['id'] . '" class="btn btn-info btn-xs">编辑</a>&nbsp;<a href="./gdlist.php?my=delete&id=' . $res['id'] . '" class="btn btn-xs btn-danger" onclick="return confirm(\'你确实要删除此记录吗？\');">删除</a></td></tr>';
                    }
                    ?>
                    </tbody>
                </table>
            </div>
            <?php
            echo '<ul class="pagination">';
            $first = 1;
            $prev = $page - 1;
            $next = $page + 1;
            $last = $pages;
            if ($page > 1) {
                echo '<li><a href="gdlist.php?page=' . $first . $link . '">首页</a></li>';
                echo '<li><a href="gdlist.php?page=' . $prev . $link . '">&laquo;</a></li>';
            } else {
                echo '<li class="disabled"><a>首页</a></li>';
                echo '<li class="disabled"><a>&laquo;</a></li>';
            }
            for ($i = 1; $i < $page; $i++)
                echo '<li><a href="gdlist.php?page=' . $i . $link . '">' . $i . '</a></li>';
            echo '<li class="disabled"><a>' . $page . '</a></li>';
            if ($pages >= 10) $pages = 10;
            for ($i = $page + 1; $i <= $pages; $i++)
                echo '<li><a href="gdlist.php?page=' . $i . $link . '">' . $i . '</a></li>';
            echo '';
            if ($page < $pages) {
                echo '<li><a href="gdlist.php?page=' . $next . $link . '">&raquo;</a></li>';
                echo '<li><a href="gdlist.php?page=' . $last . $link . '">尾页</a></li>';
            } else {
                echo '<li class="disabled"><a>&raquo;</a></li>';
                echo '<li class="disabled"><a>尾页</a></li>';
            }
            echo '</ul>';
#分页
        }
        ?>
    </div>
</div>



